﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SPMS.Log.Core;
using SPMS.Log.Repository;
using SPMS.Utility;
using SPMS.Commons.Configuration;

namespace SPMS.Log.Configuration
{
    /// <summary>
    /// 日志解析上下文
    /// </summary>
    /// <remarks>日志配置解析过程中的解析上下文</remarks>
    /// <history>
    /// [zanezeng]               2009/10/15 12:02    创建
    /// </history>
    public sealed class ParseContext
    {
        #region ---Constructor

        /// <summary>
        /// 初始化类型<see cref="ParseContext"/>的新实例
        /// </summary>
        /// <param name="repository">要配置的日志存储器对象</param>
        /// <param name="handlerFactory">节点处理器工厂</param>
        /// <param name="variableProvider">扩展变量提供者</param>
        /// <remarks></remarks>
        /// <exception cref="ArgumentNullException"><paramref name="repository"/>为<c>null</c>引用。</exception>
        /// <exception cref="ArgumentNullException"><paramref name="handlerFactory"/>为<c>null</c>引用。</exception>
        /// <exception cref="ArgumentNullException"><paramref name="variableProvider"/>为<c>null</c>引用。</exception>
        /// <history>
        /// [zanezeng]               2009/10/15 13:51    创建
        /// </history>
        public ParseContext( IConfigurableLoggingRepository repository,
                             IConfigurationNodeHandlerFactory<ParseContext> handlerFactory,
                             IExtendedVariableProvider variableProvider )
        {
            //断言参数
            ArgumentAssert.AssertArgumentNotNull( repository, "repository" );
            ArgumentAssert.AssertArgumentNotNull( handlerFactory, "repository" );
            ArgumentAssert.AssertArgumentNotNull( variableProvider, "repository" );

            //保存参数
            this.m_Repository = repository;
            this.m_HandlerFactory = handlerFactory;
            this.m_VariableProvider = variableProvider;
        }

        #endregion

        #region ---Property

        /// <summary>
        /// 获得要配置的日志存储器
        /// </summary>
        /// <value>要配置的日志存储器</value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2009/10/15 13:52    创建
        /// </history>
        public IConfigurableLoggingRepository Repository
        {
            get
            {
                return this.m_Repository;
            }
        }

        /// <summary>
        /// 获得节点处理器工厂
        /// </summary>
        /// <value>节点处理器工厂</value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-12-15 15:16    Created
        /// </history>
        public IConfigurationNodeHandlerFactory<ParseContext> HandlerFactory
        {
            get
            {
                //返回设置的节点处理器工厂
                return this.m_HandlerFactory;
            }
        }

        /// <summary>
        /// 获得扩展变量提供者
        /// </summary>
        /// <value>扩展变量提供者</value>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]               2010/1/21 15:12    创建
        /// </history>
        public IExtendedVariableProvider VariableProvider
        {
            get
            {
                //返回设置的扩展变量提供者
                return this.m_VariableProvider;
            }
        }

        #endregion

        #region ---Public Method

        /// <summary>
        /// 通过指定的配置节点获得对应的配置节点处理器
        /// </summary>
        /// <param name="node">需要处理的配置节点</param>
        /// <returns>配置节点处理器</returns>
        /// <remarks></remarks>
        /// <history>
        /// [zanezeng]   2008-11-17 8:50    Created
        /// </history>
        public IConfigurationNodeHandler<ParseContext> GetNodeHandler( IConfigurationNode node )
        {
            //判断是否为Null
            if (null == node)
            {
                return null;
            }

            //返回配置节点解析器
            return this.HandlerFactory.GetNodeHandler( node );
        }

        #endregion

        #region ---Field

        /// <summary>
        /// 要配置的日志存储器
        /// </summary>
        private IConfigurableLoggingRepository m_Repository;

        /// <summary>
        /// 配置节点解析器工厂
        /// </summary>
        private IConfigurationNodeHandlerFactory<ParseContext> m_HandlerFactory;

        /// <summary>
        /// 扩展变量提供者
        /// </summary>
        private IExtendedVariableProvider m_VariableProvider;

        #endregion
    }
}
